<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2017/5/18
 * Time: 15:33
 */

namespace Home\Service;


use Common\Common\Service;
use Home\Component\ApplyComponent;
use Home\Exception\HomeException;
use Home\Request\Apply\SubmitMsgRequest;
use Home\Response\SubmitMsgResponse;

class SubmitMsgService implements Service
{
    public function run(SubmitMsgRequest $request = null)
    {
        if ($request->issetName() == false)
            throw new HomeException(10001);
        if ($request->issetPhone() == false)
            throw new HomeException(10001);
        if ($request->issetPictureId() == false)
            throw new HomeException(10001);
        if ($request->issetIntroduce() == false)
            throw new HomeException(10001);
        if ($request->issetUserId() == false)
            throw new HomeException(10001);
        if ($request->issetActId() == false)
            throw new HomeException(10001);
        if ($request->issetAge() == false)
            throw new HomeException(10001);
        if ($request->issetAddress() == false)
            throw new HomeException(10001);

        $name = $request->getName();
        $phone = $request->getPhone();
        $picture_id = $request->getPictureId();
        $introduce = $request->getIntroduce();
        $user_id = $request->getUserId();
        $act_id = $request->getActId();
        $nickname = $request->getNickName();
        $headimgurl = $request->getHeadImgUrl();
        $age = $request->getAge();
        $address = $request->getAddress();

        $apply = array(
            'act_id' => $act_id,
            'user_id' => $user_id,
            'vote_num' => 0,
            'name' => $name,
            'phone' => $phone,
            'picture_id' => $picture_id,
            'introduce' => $introduce,
            'nickname' => $nickname,
            'headimgurl' => $headimgurl,
            'age' => $age,
            'address' => $address
        );

        $component = new ApplyComponent();
        $response = new SubmitMsgResponse();

        try {
            $data = $component->addApply($apply);
            if (!$data) {
                $response->setItIsNull();
            }
        } catch (HomeException $e) {
            throw new HomeException($e->getMessage(), $e->getCode());
        }
        return $response;
    }
}